<?php
/**
 * Class for hand authentication when 
 * login using OpenID
 * @author Tuan Ngo
 */

class AuthModel{
	function setIdentity($identity, $email, $first_name, $last_name){
		Zend_Loader::loadClass("AdminModel");
		$adminModel = new AdminModel();
		$admin_info = $adminModel->fetchRow(array("sunnet_email =?"=>trim($email)));	
		$session = new Zend_Session_Namespace();
		$identity_arr = array(
			'identity' => $identity,
			'id' => $admin_info['id'],
			'sunnet_email' => $email,
			'first_name' => $first_name,
			'last_name' =>$last_name,
/*moi sua*/           
            'role'=>$admin_info['role']
/**/	
    	);
		
		$session->identity = $identity_arr;
//        Zend_debug::dump($session->identity);die;
	}
	
	function hasIdentity(){
		$session = new Zend_Session_Namespace();
		return $session->identity ? true : false;
	}
	
	function getIdentity(){
		$session = new Zend_Session_Namespace();
		return $session->identity;
	}
	
	function clearIdentity(){
		$session = new Zend_Session_Namespace();
		unset($session->identity);
	}
	
	function getIdentityField($key){
		if($this->hasIdentity()){
			$identity = $this->getIdentity();
			return $identity[$key];
		}else 
			return NULL;
	}
	
	function validateUser($email){
		Zend_Loader::loadClass('AdminModel');
		
	//	$result = preg_match("/(.)*@gmail.com/",trim($email));
	//	Zend_Debug::dump($result);die;
	//	if($result){
			//check user is in user list or not	
/*			$validator = new Zend_Validate_Db_RecordExists(array(
																	'table'=>'admin',
																	'field'=>'sunnet_email',
																	'exclude' => 'status=1'
																	));
			if ($validator->isValid(trim($email))) {
					return true;
                
			}else 
		          	return false;
*/		
         return true;       
        
        //}
		//else 
		//	return false;
	}
}